package study.member.quiz.week5;

public class Question5Week {
	
	/**
	 * description
	 * 영어 공부 
	 * The parameter weekday is true if it is a weekday
	 * , and the parameter vacation is true if we are on vacation. 
	 * We sleep in if it is not a weekday or we're on vacation. 
	 * Return true if we sleep in. 
	 * @param weekday
	 * @param vacation
	 * @return
	 * question http://codingbat.com/prob/p187868
	 */
	public boolean sleepIn(boolean weekday, boolean vacation) {
		if(!weekday || vacation){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * We have two monkeys, a and b, and the parameters aSmile and bSmile indicate if each is smiling. We are in trouble if they are both smiling or if neither of them is smiling. Return true if we are in trouble. 
	 * @param aSmile
	 * @param bSmile
	 * @return
	 * question http://codingbat.com/prob/p181646
	 */
	public boolean monkeyTrouble(boolean aSmile, boolean bSmile) {
		if(aSmile && bSmile){
			return true;
		}
		if (!aSmile && !bSmile) {
			return true;
		}
		return false;
	}
	
	
	/**
	 * Given two int values, return their sum. Unless the two values are the same, then return double their sum.
		sumDouble(1, 2) → 3
		sumDouble(3, 2) → 5
		sumDouble(2, 2) → 8
	 * @param a
	 * @param b
	 * @return
	 */
	public int sumDouble(int a, int b) {
		int sum = a+b;
		if(a == b ){
			sum *= 2;
		}
		return sum;
	}
	
	/**
	 * absolute difference 절대차
	 * Given an int n, return the absolute difference between n and 21, except return double the absolute difference if n is over 21. 
	 * @param n
	 * @return
	 */
	public int diff21(int n) {
		int diffNum = 21;
		int ad = 0;
		if(n < diffNum){
			ad = diffNum - n;
		}else{
			ad = n - diffNum ;
			ad =  2*ad;
		}
		return ad;
	}
	
	/**
	 * We have a loud talking parrot. The "hour" parameter is the current hour time in the range 0..23.
	 * We are in trouble if the parrot is talking and the hour is before 7 or after 20. Return true if we are in trouble. 
	 * @param talking
	 * @param hour
	 * @return
	 */
	public boolean parrotTrouble(boolean talking, int hour) {
		if(talking && (hour < 7 || hour > 20)){
			return true;
		}else return false;
	}
	
	
	/**
	 * Given 2 ints, a and b, return true if one if them is 10 or if their sum is 10. 
	 * @param a
	 * @param b
	 * @return
	 */
	public boolean makes10(int a, int b) {
		boolean result = false;
		if(a%10 == 0 || b%10 == 0 || ((a+b) == 10)){
			result = true;
			return result;
		}else{
			return false;
		}
	}
	
	
	/**
	 * Given an int n, return true if it is within 10 of 100 or 200. Note: Math.abs(num) computes the absolute value of a number. 
	 * @param n
	 * @return
	 */
	public boolean nearHundred(int n) {
		return false;
	}
	
	/**
	 * 
	 * Given 2 int values, return true if one is negative and one is positive. Except if the parameter "negative" is true, then return true only if both are negative. 
	 * @param a
	 * @param b
	 * @param negative
	 * @return
	 */
	public boolean posNeg(int a, int b, boolean negative) {
		if(negative){
			if(a < 0 && b < 0) return true;
			else return false;
		}else{
			if((a > 0 && b <0) || (a < 0 && b >0)) return true;
			else{
				return false;
			}
		}
	}
	
	/**
	 * Given a string, return a new string where "not " has been added to the front. 
	 * However, if the string already begins with "not", return the string unchanged. Note: use .equals() to compare 2 strings. 
	 * @param str
	 * @return
	 */
	public static String notString(String str) {
		
		if(str.startsWith("not")){
			return str;
		}
		else return "not " + str;
		
	}
/*
	Given a non-empty string and an int n,
	return a new string where the char at index n has been removed. 
	The value of n will be a valid index of a char in the original string (i.e. n will be in the range 0..str.length()-1 inclusive). 
	missingChar("kitten", 1) → "ktten"
	missingChar("kitten", 0) → "itten"
	missingChar("kitten", 4) → "kittn"
	
*/
	public String missingChar(String str, int n) {
		StringBuffer missingChar = new StringBuffer();
		for(int i=0;i<str.length();i++){
			if(n != i){
				missingChar.append(str.charAt(i));
			}
		}
		return missingChar.toString();
	}
	/*
	Given a string, return a new string where the first and last chars have been exchanged.
	frontBack("code") → "eodc"
	frontBack("a") → "a"
	frontBack("ab") → "ba"
	*/
	public String frontBack(String str) {
		if(str.length()<=1) return str;
		String mid = str.substring(1, str.length()-1);
		//last + mid + first
		return str.charAt(str.length()-1) + mid + str.charAt(0);
	}

/*
 * Given a string, we'll say that the front is the first 3 chars of the string. 
 * If the string length is less than 3, the front is whatever is there. 
 * Return a new string which is 3 copies of the front. 
	front3("Java") → "JavJavJav"
	front3("Chocolate") → "ChoChoCho"
	front3("abc") → "abcabcabc"
*/
	public String front3(String str) {
		//bed case programing
		String tmp = "";
		int strLength = str.length();
		switch(strLength){
			case 0 :
				
			break;
			case 1 :
				tmp = str.substring(0,1);	
				break;
			case 2 :
				tmp = str.substring(0,2);	
				break;
			default :
				tmp = str.substring(0,3);	
				break;
		}
		return tmp + tmp + tmp;
	}
	/*
	Given a string, take the last char and return a new string with the last char added at the front and back, so "cat" yields "tcatt". The original string will be length 1 or more. 
	backAround("cat") → "tcatt"
	backAround("Hello") → "oHelloo"
	backAround("a") → "aaa"
	 */
	public String backAround(String str) {
		char backAroundstr = str.charAt(str.length()-1);
		return backAroundstr + str +backAroundstr;
	}
	
	
	/*
	Return true if the given non-negative number is a multiple of 3 or a multiple of 5. Use the % "mod" operator -- see Introduction to Mod 
	or35(3) → true
	or35(10) → true
	or35(8) → false
	*/
	public boolean or35(int n) {
		return n%3 ==0 || n%5 ==0;
	}
	

	/*
	Return true if the given non-negative number is 1 or 2 more than a multiple of 20. See also: Introduction to Mod 
	more20(20) → false
	more20(21) → true
	more20(22) → true
	*/
	public boolean more20(int n) {
	return n%20 == 1 || n %20==2;	  
	}
	
	/*
	Return true if the given non-negative number is a multiple of 3 or 5, but not both. Use the % "mod" operator -- see Introduction to Mod 
	
	old35(3) → true
	old35(10) → true
	old35(15) → false
	*/
	public boolean old35(int n) {
		return n%3 == 0 ^ n%5 ==0;
	}
	
	
	public String conCat(String a, String b) {
		return a.concat(b);
	}
	
	/*	
	Given a string of even length, return the first half. So the string "WooHoo" yields "Woo". 
	firstHalf("WooHoo") → "Woo"
	firstHalf("HelloThere") → "Hello"
	firstHalf("abcdef") → "abc"
	*/
	public String firstHalf(String str) {
		int strLength = 0;
		strLength = str.length();
		return str.substring(0, strLength/2);
	}
	

	/* 
	Given a string and an index, return a string length 2 starting at the given index. If the index is too big or too small to define a string length 2, use the first 2 chars. The string length will be at least 2. 
	twoChar("java", 0) → "ja"
	twoChar("java", 2) → "va"
	twoChar("java", 3) → "ja"
	 */
	public String twoChar(String str, int index) {
		String towCharstr = "";
		try{
			towCharstr = str.substring(index, 2);
		}catch(Exception e){
			towCharstr = str.substring(0, 2);
		}
		return towCharstr;
	}
	
	public String twoChar2(String str, int index) {
		String towChar2 = "";
		if(index+2 <= str.length() && index > 0){
			
			towChar2 =str.substring(index,index+2);
		}else{
			
			towChar2 =str.substring(0,2);
		}
		return towChar2;
	}

	
	
	
	public static void main(String[] args) {
		String str = "abc";
		System.out.println(str.length());
		System.out.println(str.substring(0, 2));
		System.out.println(str.substring(1, 1+2));
	}

	
}
